package me.hunche.app.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import me.hunche.app.dao.UserDao;
import me.hunche.app.domain.Activity;
import me.hunche.app.domain.ActivityMember;
import me.hunche.app.domain.User;
import me.hunche.app.service.NotifyService;
import me.hunche.app.service.SMSService;
import me.hunche.app.service.ShortUrlService;
import me.hunche.app.util.DateHelper;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;



@Service("notifyService")
public class NotifyServiceImpl implements NotifyService {
	
	private static final Logger logger = Logger.getLogger(NotifyServiceImpl.class);
	private Locale locale =  new Locale("zh", "CN");
	
	@Autowired UserDao ud;
	@Autowired private MessageSource messageSource;
	@Autowired private SMSService smsService;
	@Autowired private UserDao userDao;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("M月d号");
	

	public void recruit(Activity a) {
		List<User> users = userDao.findByTeamId(a.getTeamId());
		List<String> recipients = new ArrayList<String>();
		for(User u:users){
			recipients.add(u.getMobile());
		}
		SimpleDateFormat sdf = new SimpleDateFormat("M月d号");
		String time = sdf.format(a.getActivityDate());
		String content = messageSource.getMessage("recruit", new Object[]{time,a.getAmount(),a.getRequires(),a.getLine()}, locale);
		smsService.sendMessage(recipients, content);
	}
	

	public void recruit2(Activity a, List<User> users) {
		List<String> recipients = new ArrayList<String>();
		for(User u:users){
			recipients.add(u.getMobile());
		}
		SimpleDateFormat sdf = new SimpleDateFormat("M月d号");
		String time = sdf.format(a.getActivityDate());
		String content = messageSource.getMessage("recruit", new Object[]{time,a.getAmount(),a.getRequires(),a.getLine()}, locale);
		smsService.sendMessage(recipients, content);
	}

	public void result(Activity a, User user) {
		SimpleDateFormat sdf = new SimpleDateFormat("M月d号");
		String time = sdf.format(a.getActivityDate());
		String content = messageSource.getMessage("result", new Object[]{time,a.getAmount(),a.getAllMembers().size()}, locale);
		smsService.sendMessage(user.getMobile(), content);
	}

	public void created(Activity a) {
		// TODO Auto-generated method stub
		
	}

	public void confirmed(Activity a) {
		
		String content0 = messageSource.getMessage("confirmed.0", new Object[]{sdf.format(a.getActivityDate())}, locale);
		String content1 = messageSource.getMessage("confirmed.1", new Object[]{sdf.format(a.getActivityDate())}, locale);
		String content2 = messageSource.getMessage("confirmed.2", new Object[]{sdf.format(a.getActivityDate())}, locale);
		
		smsService.sendMessage(a.getLeader().getMobile(),content2);
		
		List<String> recipients = new ArrayList<String>();
		for(ActivityMember m: a.getMembers()){
			recipients.add(m.getMobile());
		}
		
		smsService.sendMessage(recipients,content1);
		
		if(a.getReserveMembers()!=null){
			List<String> recipients2 = new ArrayList<String>();
			for(ActivityMember m: a.getReserveMembers()){
				recipients2.add(m.getMobile());
			}
			smsService.sendMessage(recipients2,content0);
		}
	}
	

	public void confirmed(Activity a, ActivityMember m) {
		String content0 = messageSource.getMessage("confirmed.0", new Object[]{sdf.format(a.getActivityDate())}, locale);
		String content1 = messageSource.getMessage("confirmed.1", new Object[]{sdf.format(a.getActivityDate())}, locale);
		String content2 = messageSource.getMessage("confirmed.2", new Object[]{sdf.format(a.getActivityDate())}, locale);
		
		if(ActivityMember.TYPE_RESERVE == m.getType()){
			String content = messageSource.getMessage("confirmed.0", new Object[]{sdf.format(a.getActivityDate())}, locale);
			smsService.sendMessage(m.getMobile(),content);
		}else if(ActivityMember.TYPE_REGULAR == m.getType() ){
			String content = messageSource.getMessage("confirmed.1", new Object[]{sdf.format(a.getActivityDate())}, locale);
			smsService.sendMessage(m.getMobile(),content);
		}else if(ActivityMember.TYPE_LEADER == m.getType()){
			String content = messageSource.getMessage("confirmed.2", new Object[]{sdf.format(a.getActivityDate())}, locale);
			smsService.sendMessage(m.getMobile(),content);
		}else{
			logger.warn("confirmed invalid type "+m.getType());
		}
		
	}

	public void canceled(Activity a) {
		// TODO Auto-generated method stub
		
	}

	public void remind(Activity a) {
		long days = (a.getActivityDate().getTime() - DateHelper.dayStart(DateHelper.now()).getTime()) / (24*60*60*1000l);
		
		String sdays =null;
		if(days == 7l){
			sdays = "一周";
		}else if(days == 2){
			sdays = "两天";
		}else {
			sdays = days+"天";
		}
		
		
//		int days = Long.valueOf((a.getActivityDate().getTime()-DateHelper.dayStart(DateHelper.now()).getTime())/864000000).intValue();
		
		SimpleDateFormat sdf = new SimpleDateFormat("M月d号");
		String content = messageSource.getMessage("remind", new Object[]{sdf.format(a.getActivityDate()),sdays}, locale);
		List<String> recipients = new ArrayList<String>();
		if(a.getLeader()!=null){
			recipients.add(a.getLeader().getMobile());
		}
		if(a.getMembers()!=null && !a.getMembers().isEmpty()){
			for(ActivityMember m:a.getMembers()){
				recipients.add(m.getMobile());
			}
		}
		smsService.sendMessage(recipients, content);
		
		if(logger.isInfoEnabled()){
			logger.info("sendMessage "+Arrays.toString(recipients.toArray()) + " " +content);
		}
		
	}

	public void ready(Activity a) {
		SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
		String content = messageSource.getMessage("ready", new Object[]{a.getLeader().getNickname(),a.getLeader().getMobile(),sdf.format(a.getRendezvousTime()),a.getRendezvous()}, locale);
		List<String> recipients = new ArrayList<String>();
		if(a.getLeader()!=null){
			recipients.add(a.getLeader().getMobile().toString());
		}
		if(a.getMembers()!=null && !a.getMembers().isEmpty()){
			for(ActivityMember m:a.getMembers()){
				recipients.add(m.getMobile());
			}
		}
		smsService.sendMessage(recipients, content);
		
		if(logger.isInfoEnabled()){
			logger.info("sendMessage "+Arrays.toString(recipients.toArray()) + " " +content);
		}
	}


	public void hasReady(Activity a) {
		// TODO Auto-generated method stub
		
	}

	@Autowired
	private ShortUrlService surlService;
	
	public void go(Activity a) {
		SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
		Date ndate = DateHelper.add(a.getRendezvousTime(), Calendar.HOUR, -1);
		String sgat = "一小时";
		
		
		
//		List<String> recipients = new ArrayList<String>();
		if(a.getLeader()!=null){
//			recipients.add(a.getLeader().getUser().getMobile().toString());
			a.getMembers().add(a.getLeader());
		}
		
		if(a.getMembers()!=null && !a.getMembers().isEmpty()){
			for(ActivityMember m:a.getMembers()){
				String longUrl = "http://cqfiat.com:8080/m-car/members/"+m.getId()+"/gone.html";
				String shortUrl = surlService.long2short(longUrl).replaceAll("http://", "");
				String content = messageSource.getMessage("go.1", new Object[]{sdf.format(a.getRendezvousTime()),sgat,a.getRendezvous(),a.getLeader().getNickname(),a.getLeader().getMobile(),shortUrl}, locale);
				smsService.sendScheduledMessage(m.getMobile(), content, ndate);
			}
		}

		if(logger.isInfoEnabled()){
			logger.info("go "+a.getId());
		}
	}

	public void go2(Activity a) {
		SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
		Date ndate = DateHelper.add(a.getRendezvousTime(), Calendar.HOUR, -1);
		String sgat = "一小时";
		
		String content = messageSource.getMessage("go", new Object[]{sdf.format(a.getRendezvousTime()),sgat,a.getRendezvous(),a.getLeader().getNickname(),a.getLeader().getMobile()}, locale);
		List<String> recipients = new ArrayList<String>();
		if(a.getLeader()!=null){
			recipients.add(a.getLeader().getMobile().toString());
		}
		if(a.getMembers()!=null && !a.getMembers().isEmpty()){
			for(ActivityMember m:a.getMembers()){
				recipients.add(m.getMobile());
			}
		}
		smsService.sendScheduledMessage(recipients, content, ndate);
		
		if(logger.isInfoEnabled()){
			logger.info("sendMessage "+Arrays.toString(recipients.toArray()) + " " +content);
		}
	}


	public void hasLeave(Activity a) {
		// TODO Auto-generated method stub
		
	}


	public void upgrade(Activity a,ActivityMember member) {
		String date = sdf.format(a.getActivityDate());
		
		if(logger.isDebugEnabled()){
			logger.debug("@upgrade "+a.getId()+ "("+date+")" +member.getNickname() +" - start");
		}
		
		String content = messageSource.getMessage("upgrade", new Object[]{sdf.format(a.getActivityDate())}, locale);
		
		smsService.sendMessage(member.getMobile(), content);
		
		
		if(logger.isInfoEnabled()){
			logger.info("@upgrade "+a.getId()+ "("+date+")" +member.getNickname() +" - end");
		}
	}





}
